package com.projectx.util;

import android.app.Activity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ActivityManage {
    /**
     * 存放Activity的map
     */
    private static Map<String, Activity> activities = new HashMap<String, Activity>();

    /**
     * 获取管理类中注册的所有Activity的map
     * @return
     */
    public static Map<String, Activity> getActivities()
    {
        return activities;
    }

    /**
     * 根据键值取对应的Activity
     * @param key 键值
     * @return 键值对应的Activity
     */
    public static Activity getActivity(String key)
    {
        return activities.get(key);
    }

    /**
     * 注册Activity
     * @param value
     * @param key
     */
    public static void addActivity(String key,Activity value)
    {
        activities.put(key, value);
    }

    /**
     * 将key对应的Activity移除掉
     * @param key
     */
    public static void removeActivity(String key)
    {
        activities.remove(key);
    }

    /**
     * 将key对应的Activity移除并关闭
     * @param key
     */
    public static void exitActivity(String key) {
        if (ActivityManage.getActivity(key) != null) {
            ActivityManage.getActivity(key).finish();
            activities.remove(key);
        }
    }

    /**
     * finish掉所有的Activity移除所有的Activity
     */
    public static void removeAllActivity()
    {
        Iterator<Activity> iteratorActivity = activities.values().iterator();
        while(iteratorActivity.hasNext()){
            iteratorActivity.next().finish();
        }
        activities.clear();
    }

    /**
     * finish掉所有的Activity移除所有的Activity
     * @param key
     */
    public static void removeAllActivityExcept(String key)
    {
        Activity a = ActivityManage.getActivity(key);
        for (Activity activity : activities.values()) {
            if (activity != a) {
                activity.finish();
            }
        }
        activities.clear();
        ActivityManage.addActivity(key,a);
    }

    /**
     * 清空activities
     */
    public static void clear(){
        activities = new HashMap<String, Activity>();
    }
}
